#include "common.h"
#include "spi0.h"

int spi0::spi0fd = 0;
int spi0::CS0Valuefd = 0;
unsigned char spi0::mode = 0;
unsigned char spi0::bits = 8;
unsigned int spi0::speed = 1000000;



void spi0::spiInit()
{
    int ret = 0;
    spi0fd = open(SPI0DEV_NAME, O_RDWR);
    if (spi0fd < 0)
        printf("can't open device\n");

    /* spi mode */
    ret = ioctl(spi0fd, SPI_IOC_WR_MODE, &mode);
    if (ret == -1)
        printf("can't set spi mode\n");

    ret = ioctl(spi0fd, SPI_IOC_RD_MODE, &mode);
    if (ret == -1)
        printf("can't get spi mode\n");

    /* bits per word */
    ret = ioctl(spi0fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
    if (ret == -1)
        printf("can't set bits per word\n");

    ret = ioctl(spi0fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
    if (ret == -1)
        printf("can't get bits per word\n");

    /* max speed hz */
    ret = ioctl(spi0fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
    if (ret == -1)
        printf("can't set max speed hz\n");

    ret = ioctl(spi0fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
    if (ret == -1)
        printf("can't get max speed hz\n");

    printf("spi mode: %d\n", mode);
    printf("bits per word: %d\n", bits);
    printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
    printf("over!\n");
}

void spi0::spi0_CS0_init()
{
    int exportGpio;
    //write only 只写 只有先向export写入数字，/sys/class/gpio下才能生成gpioXXX文件
	exportGpio = open("/sys/class/gpio/export", O_WRONLY);
	if (exportGpio < 0)
	{
		puts("Cannot open GPIO to export it\n");
		exit(1);
	}
   write(exportGpio, "449", 4);//string -> const char *
   close(exportGpio);

    // Update the direction of the GPIO to be an output
	int directionfd;
    directionfd = open("/sys/class/gpio/gpio449/direction", O_RDWR);
	if (directionfd < 0)
	{
		puts("Cannot open GPIO direction it\n");
		exit(1);
	}
	write(directionfd, "out", 4);
	close(directionfd);

   
   CS0Valuefd = open("/sys/class/gpio/gpio449/value", O_RDWR);
   if (CS0Valuefd < 0)
	{
		puts("Cannot open GPIO value\n");
		exit(1);
	}
   write(CS0Valuefd,"1", 2);
}
//CS0 拉低
void spi0::spi0_CS0_enable()
{
    write(CS0Valuefd,"0", 2);
}
//CS0 拉高
void spi0::spi0_CS0_disable()
{
    write(CS0Valuefd,"1", 2);
}


spi0::spi0(/* args */)
{
    
}

spi0::~spi0()
{
}

int spi0::SPI_Transfers(unsigned char *tx,unsigned char *rx,unsigned int len)
{
    struct spi_ioc_transfer tr;
    tr.tx_buf = (unsigned long)tx;
    tr.rx_buf = (unsigned long)rx;
    tr.len = len;
    tr.delay_usecs = 0;
    tr.speed_hz = 0;
    tr.bits_per_word = 8;
    int ret;
    printf("spi0fd:%d\n",spi0fd);
    printf("tx:%d\n",tx[0]);
    printf("tx:%d\n",tx[5]);
    printf("len:%d\n",len);
    ret = ioctl(spi0fd, SPI_IOC_MESSAGE(1), &tr); //SPI_IOC_MESSAGE(1)，全双工  
    printf("spi treasfer ret:%d\n",ret);
    if (ret != len)
    {
        printf("can't send spi message\n");
        return -1;
    }
    else
    {
        return ret;
    }
}